<?php
// 配置文件 开始
include_once APP_PATH . '/index.php';
include_once APP_PATH . '/config/index.php';
// 配置文件 结束

// 设置 sa0Tool 信息 开始
define(SA0_TOOL_TITLE, 'sa0Tool');
define(SA0_TOOL_VERSION, 'P04');
define(SA0_TOOL_AUTHOR, 'sa0ChunLuyu');
define(SA0_TOOL_CONTENT, 'HAPPY HACKING');
// 设置 sa0Tool 信息 结束

// 分割路由 开始
$router_ = routerMaker();
$fileName_ = $router_[0];
$controllerShortName_ = $router_[1];
$controllerName_ = $router_[1] . 'Controller';
$functionName_ = $router_[2];
// 分割路由 结束

// 配置全局设置 开始
define(APP_CONFIG, $config_);
define(CONTROLLER_SHORT_NAME, $controllerShortName_);
define(FUNCTION_NAME, $functionName_);
// 配置全局设置 结束

// 配置跨域设置 开始
if (checkOrigin()) {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS');
    header('Access-Control-Allow-Headers: ' . implode(',', headerMaker()));
}
// 配置跨域设置 结束

// 配置请求类型 开始
if (!checkMethods()) methodsError();
// 配置请求类型 结束

// 设置时区 开始
date_default_timezone_set(timezoneMaker());
// 设置时区 结束

// 引入插件工具 开始
foreach (toolMaker() as $tool_value) {
    if (is_file(__DIR__ . '/tool/' . $tool_value . '.php')) include_once __DIR__ . '/tool/' . $tool_value . '.php';
    if (is_file(__DIR__ . '/tool/' . $tool_value . '.php')) continue;
    if (is_file(APP_PATH . '/tool/' . $tool_value . '.php')) include_once APP_PATH . '/tool/' . $tool_value . '.php';
}
include __DIR__ . '/tool/smarty/libs/Smarty.class.php';
// 引入插件工具 结束

// 文件 开始
if (substr($controllerName_, 0, 2) === '__') controllerAccessError();
if (!is_file($fileName_)) fileError();
include_once __DIR__ . '/sa0Tool.php';
include_once $fileName_;
$controller_ = new $controllerName_();
// 文件 结束

// 函数 开始
if (substr($functionName_, 0, 2) === '__') functionAccessError();
if (!method_exists($controller_, $functionName_)) functionError();
$controller_->$functionName_();
// 函数 结束

function methodsError()
{
    // 请求类型错误
    errorDo("好像这个接口不允许{$_SERVER["REQUEST_METHOD"]}访问呢");
}

function functionAccessError()
{
    // 函数权限错误
    errorDo('好像这里的服务不是对外开放的呢');
}

function functionError()
{
    // 函数不存在
    errorDo('看起来是没有找到您需要的服务呢');
}

function fileError()
{
    // 文件不存在
    errorDo('看起来是没有找到您需要的接口呢');
}

function controllerAccessError()
{
    // 控制器权限错误
    errorDo('好像这里的接口不是对外开放的呢');
}

function errorDo($error_massage)
{
    // 报错页面
    define(ERROR_MESSAGE, $error_massage);
    include_once __DIR__ . '/views/error.html';
    exit;
}

function toolMaker()
{
    $tool_ = [];
    if (!isset(APP_CONFIG['tool'])) return $tool_;
    if (isset(APP_CONFIG['tool']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME])) $tool_ = array_merge($tool_, APP_CONFIG['tool']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME]);
    if (isset(APP_CONFIG['tool']['controller'][CONTROLLER_SHORT_NAME]['global'])) $tool_ = array_merge($tool_, APP_CONFIG['tool']['controller'][CONTROLLER_SHORT_NAME]['global']);
    if (isset(APP_CONFIG['tool']['global'])) $tool_ = array_merge($tool_, APP_CONFIG['tool']['global']);
    return array_unique($tool_);
}

function timezoneMaker()
{
    // 生成时区
    if (!isset(APP_CONFIG['timezone'])) return 'PRC';
    if (isset(APP_CONFIG['timezone']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME])) return APP_CONFIG['timezone']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME];
    if (isset(APP_CONFIG['timezone']['controller'][CONTROLLER_SHORT_NAME]['global'])) return APP_CONFIG['timezone']['controller'][CONTROLLER_SHORT_NAME]['global'];
    if (isset(APP_CONFIG['timezone']['global'])) return APP_CONFIG['timezone']['global'];
    return "PRC";
}

function checkOrigin()
{
    // 检查是否打开跨域
    if (!isset(APP_CONFIG['origin']) || !isset(APP_CONFIG['origin']['origin'])) return false;
    if (isset(APP_CONFIG['origin']['origin']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME])) return APP_CONFIG['origin']['origin']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME];
    if (isset(APP_CONFIG['origin']['origin']['controller'][CONTROLLER_SHORT_NAME]['global'])) return APP_CONFIG['origin']['origin']['controller'][CONTROLLER_SHORT_NAME]['global'];
    if (isset(APP_CONFIG['origin']['origin']['global'])) return APP_CONFIG['origin']['origin']['global'];
    return false;
}

function headerMaker()
{
    // 生成Header内容
    if (!isset(APP_CONFIG['origin']) || !isset(APP_CONFIG['origin']['headers'])) return ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'];
    if (isset(APP_CONFIG['origin']['headers']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME])) return APP_CONFIG['origin']['headers']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME];
    if (isset(APP_CONFIG['origin']['headers']['controller'][CONTROLLER_SHORT_NAME]['global'])) return APP_CONFIG['origin']['headers']['controller'][CONTROLLER_SHORT_NAME]['global'];
    if (isset(APP_CONFIG['origin']['headers']["global"])) return APP_CONFIG['origin']['headers']['global'];
    return ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'];
}

function checkMethods()
{
    // 检查请求类型是否符合要求
    $methods_ = $_SERVER['REQUEST_METHOD'];
    if (!isset(APP_CONFIG['origin']) || !isset(APP_CONFIG['origin']['methods'])) return false;
    if (isset(APP_CONFIG['origin']['methods']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME])) return in_array($methods_, APP_CONFIG["origin"]['methods']['controller'][CONTROLLER_SHORT_NAME]['function'][FUNCTION_NAME]);
    if (isset(APP_CONFIG['origin']['methods']['controller'][CONTROLLER_SHORT_NAME]['global'])) return in_array($methods_, APP_CONFIG['origin']['methods']['controller'][CONTROLLER_SHORT_NAME]['global']);
    if (isset(APP_CONFIG['origin']['methods']['global'])) return in_array($methods_, APP_CONFIG['origin']['methods']['global']);
    return false;
}

function routerMaker()
{
    // 路由分析
    $pathArray = explode('/', $_GET['routeInfo']);
    if (count($pathArray) == 1) {
        return [APP_PATH . '/function/' . $pathArray[0] . '.php', $pathArray[0], 'index'];
    };
    $filePath = '';
    for ($i = 0; $i < count($pathArray) - 1; $i++) {
        $filePath .= '/' . $pathArray[$i];
    }
    return [APP_PATH . '/controller' . $filePath . '.php', $pathArray[count($pathArray) - 2], $pathArray[count($pathArray) - 1]];
}
